#include <cstdio>

int s1[ 100001 ], s2[ 100001 ], ag[ 100001 ];

int find( int root, int *s )
{
    if ( s[ root ] == -1 )
        return root;
    return s[ root ] = find( s[ root ], s );
}

void unionSet( int root1, int root2, int *s )
{
    root1 = find( root1, s );
    root2 = find( root2, s );
    if ( root1 != root2 )
        s[ root1 ] = root2;
}

int n, m, st;

void work( )
{
    int i, u, v;
    bool flag = false;
    scanf("%d%d%d", &n, &m, &st);
    for ( i = 0; i < n; i++ )
        ag[ i ] = s1[ i ] = s2[ i ] = -1;
    for ( i = 0; i < m; i++ )
    {
        scanf("%d%d", &u, &v);
        unionSet( u, v, s1 );
        if ( find( u, s2 ) == find( v, s2 ) ) flag = true;
        if ( ag[ u ] != -1 )
            unionSet( ag[ u ], v, s2 );
        if ( ag[ v ] != -1 )
            unionSet( ag[ v ], u, s2 );
        ag[ u ] = v; ag[ v ] = u;
    }
    if ( n == 1 )
    {
        puts("YES");
        return ;
    }
    if ( !flag )
        puts("NO");
    else
    {
        for ( i = 0; i < n; i++ )
            if ( find( i, s1 ) != find( st, s1 ) )
            {
                puts("NO");
                return ;
            }
        puts("YES");
    }
}

int main( )
{
    int t, cas = 1;
    scanf("%d", &t);
    while ( t-- )
    {
        printf("Case %d: ", cas++);
        work( );
    }
    return 0;
}
